package abq2.jic.jic.plugin.operate.lostandfound;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.IListView;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

import javax.json.Json;
import java.util.HashMap;

/**
 * 标准单据列表插件
 */
public class AIQuery extends AbstractListPlugin implements Plugin {
    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        String operateKey = ((FormOperate) e.getSource()).getOperateKey();
        if (operateKey.equals("aiquery")){
            IListView list = (IListView) this.getView();
            ListSelectedRow currentSelectedRowInfo = list.getCurrentSelectedRowInfo();
            Object primaryKeyValue = currentSelectedRowInfo.getPrimaryKeyValue();

            JSONObject all = new JSONObject();

            DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("abq2_lostandfound",
                    "creator," +//用户
                            "id," +//基础资料id
                            "number," + //基础资料编号
                            "abq2_object_name," +//物品名称
                            "abq2_place," +//对应地点
                            "abq2_connection," +//联系方式
                            "abq2_notice," +//物品说明
                            "abq2_picturefield," +//图片
                            "abq2_type",//类型
                    new QFilter[]{new QFilter("id",QCP.equals, primaryKeyValue)});


            JSONObject myObject = new JSONObject();
            //指定类型
            String type = dynamicObject.getString("abq2_type");
            all.put("type",type);
            //指定物品名称
            myObject.put("name",dynamicObject.getString("abq2_object_name"));
            //指定地点
            myObject.put("place",dynamicObject.getString("abq2_place"));
            //指定注意点
            myObject.put("notice",dynamicObject.getString("abq2_notice"));
            //
            all.put("myObject",myObject.toJSONString());

            if (type.equals("丢失")){
                DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_lostandfound",
                        "creator," +//用户
                                "id," +//基础资料id
                                "number," + //基础资料编号
                                "abq2_object_name," +//物品名称
                                "abq2_place," +//对应地点
                                "abq2_connection," +//联系方式
                                "abq2_notice," +//物品说明
                                "abq2_picturefield," +//图片
                                "abq2_type",//类型
                        new QFilter[]{new QFilter("abq2_type",QCP.equals, "拾取")});
                JSONArray jsonArray = new JSONArray();
                for (DynamicObject dy : dys) {
                    JSONObject optionObject = new JSONObject();
                    optionObject.put("name",dy.getString("abq2_object_name"));
                    optionObject.put("place",dy.getString("abq2_place"));
                    optionObject.put("connection",dy.getString("abq2_connection"));
                    optionObject.put("notice",dy.getString("abq2_notice"));
                    optionObject.put("url",dy.getString("abq2_picturefield"));
                    jsonArray.add(optionObject);
                }
                all.put("foundObjects",jsonArray);

            } else if (type.equals("拾取")) {
                DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_lostandfound",
                        "creator," +//用户
                                "id," +//基础资料id
                                "number," + //基础资料编号
                                "abq2_object_name," +//物品名称
                                "abq2_place," +//对应地点
                                "abq2_connection," +//联系方式
                                "abq2_notice," +//物品说明
                                "abq2_picturefield," +//图片
                                "abq2_type",//类型
                        new QFilter[]{new QFilter("abq2_type",QCP.equals, "丢失")});
                JSONArray jsonArray = new JSONArray();
                for (DynamicObject dy : dys) {
                    JSONObject optionObject = new JSONObject();
                    optionObject.put("name",dy.getString("abq2_object_name"));
                    optionObject.put("place",dy.getString("abq2_place"));
                    optionObject.put("connection",dy.getString("abq2_connection"));
                    optionObject.put("notice",dy.getString("abq2_notice"));
                    optionObject.put("url",dy.getString("abq2_picturefield"));
                    jsonArray.add(optionObject);
                }
                all.put("lostObjects",jsonArray);
            }

            String pageId = this.getView().getMainView().getPageId();
            Object pkValue = Long.parseLong("2017054681276088320");
            DispatchServiceHelper.invokeBizService("ai", "gai", "GaiService","selectProcessInSideBar",pkValue,pageId,"");
            DispatchServiceHelper.invokeBizService("ai", "gai", "GaiService","startProcessInSideBar",pkValue,pageId,new HashMap(),all.toJSONString());
        }
    }
}